package ace.module.product.core.impl.manager.plugin.impl;

import ace.cmp.core.model.R;
import ace.cmp.util.AceStreamUtils;
import ace.module.common.api.model.input.TenantAndAppTypeInput;
import ace.module.product.api.enums.PmsStockAndSalesQuantityWmsAppTypeEnum;
import ace.module.product.api.model.bo.PmsProductStockDetailBo;
import ace.module.product.api.model.bo.PmsStockBo;
import ace.module.product.api.model.input.PmsProductStockInitOrSetInput;
import ace.module.product.core.impl.manager.plugin.PmsStockPluginManager;
import ace.module.stock.api.WmsStockApi;
import ace.module.stock.api.model.dto.WmsStockDto;
import ace.module.stock.api.model.dto.ext.WmsStockDetailDto;
import ace.module.stock.api.model.input.WmsBizInitInput;
import ace.module.stock.api.model.input.WmsBizInitObjectInput;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 商品库存插件管理者.
 *
 * @author Caspar
 *
 */
@AllArgsConstructor
@Component
public class PmsStockPluginManagerImpl implements PmsStockPluginManager {

  private final WmsStockApi wmsStockApi;

  @Override
  public PmsProductStockDetailBo initOrSet(PmsProductStockInitOrSetInput input) {
    List<WmsBizInitObjectInput> objects = input.getStocks()
        .stream()
        .map(pmsProductSkuStockInput -> {
          return WmsBizInitObjectInput.builder()
              .objectId(pmsProductSkuStockInput.getSkuId())
              .quantity(pmsProductSkuStockInput.getQuantity())
              .build();
        })
        .collect(Collectors.toList());

    WmsBizInitInput wmsBizInitInput = WmsBizInitInput.builder()
        .objectParentId(input.getProductId())
        .objects(objects)
        .build();

    TenantAndAppTypeInput<WmsBizInitInput> wmsStockInput = TenantAndAppTypeInput.<WmsBizInitInput>builder()
        .tenantId(input.getTenantId())
        .type(PmsStockAndSalesQuantityWmsAppTypeEnum.PRODUCT.getCode())
        .data(wmsBizInitInput)
        .build();

    WmsStockDetailDto wmsStockDetailDto = wmsStockApi.initOrSet(wmsStockInput).check();

    return this.convertPmsProductStockDetailBo(input.getPmsAppId(), wmsStockDetailDto);
  }

  @Override
  public R<Long> add(long tenantId, String userSn, long skuId, Long quantity) {

    return null;
  }

  @Override
  public R<Long> add(long tenantId, long skuId, Long quantity) {
    return null;
  }

  @Override
  public R<Long> subtract(long tenantId, String userSn, long skuId, Long quantity) {
    return null;
  }

  @Override
  public R<Long> subtract(long tenantId, long skuId, Long quantity) {
    return null;
  }

  @Override
  public R<Long> reverse(long tenantId, String userSn) {
    return null;
  }

  @Override
  public R<Long> reverse(long tenantId, Long recordId) {
    return null;
  }

  @Override
  public List<PmsStockBo> getSkuByTenantIdAndProductSkuId(long tenantId, List<Long> productSkuIds) {
    return null;
  }

  @Override
  public List<PmsStockBo> getSkuByTenantIdAndProductId(long tenantId, long productId) {
    return null;
  }

  @Override
  public PmsProductStockDetailBo getProductDetailByTenantIdAndProductId(long tenantId, long productId) {
    return null;
  }

  @Override
  public PmsStockBo getProductByTenantIdAndProductId(long tenantId, long productId) {
    return null;
  }

  private PmsProductStockDetailBo convertPmsProductStockDetailBo(Long pmsAppId, WmsStockDetailDto wmsStockDetailDto) {
    if (Objects.isNull(wmsStockDetailDto)) {
      return null;
    }
    PmsStockBo productStock = this.convertPmsStockBo(pmsAppId, wmsStockDetailDto.getStock());
    List<PmsStockBo> productSkuStocks = this.convertPmsStockBo(pmsAppId, wmsStockDetailDto.getChildren());
    return PmsProductStockDetailBo.builder()
        .productStock(productStock)
        .productSkuStocks(productSkuStocks)
        .build();
  }

  private List<PmsStockBo> convertPmsStockBo(Long pmsAppId, List<WmsStockDto> wmsStockDtos) {
    return AceStreamUtils.ofNullable(wmsStockDtos)
        .map(p -> this.convertPmsStockBo(pmsAppId, p))
        .collect(Collectors.toList());
  }

  private PmsStockBo convertPmsStockBo(Long pmsAppId, WmsStockDto wmsStockDto) {
    return PmsStockBo.builder()
        .pmsAppId(pmsAppId)
        .productId(wmsStockDto.getObjectParentId())
        .skuId(wmsStockDto.getObjectId())
        .quantity(wmsStockDto.getQuantity())
        .deleteFlag(wmsStockDto.getDeleteFlag())
        .createTime(wmsStockDto.getCreateTime())
        .updateTime(wmsStockDto.getUpdateTime())
        .rowVersion(wmsStockDto.getRowVersion())
        .build();
  }
}
